package com.videoplatform.controller;

import com.videoplatform.entity.Video;
import com.videoplatform.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
public class HomeController {
    
    @Autowired
    private VideoService videoService;
    
    /**
     * 主页
     */
    @GetMapping("/")
    public String home(Model model) {
        try {
            List<Video> videos = videoService.getAllActiveVideos();
            Map<String, Object> stats = videoService.getPlatformStats();
            
            model.addAttribute("videos", videos != null ? videos : new ArrayList<>());
            model.addAttribute("stats", stats);
            model.addAttribute("pageTitle", "视频平台 - 首页");
            
            return "index";
            
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "加载视频列表失败: " + e.getMessage());
            model.addAttribute("pageTitle", "视频平台 - 首页");
            model.addAttribute("videos", new ArrayList<>());
            model.addAttribute("stats", new java.util.HashMap<>());
            return "index"; // 即使有错误也尝试返回index页面，以便调试
        }
    }
    
    /**
     * 视频播放页面
     */
    @GetMapping("/video/{id}")
    public String playVideo(@PathVariable Long id, Model model) {
        try {
            Optional<Video> videoOpt = videoService.getVideoById(id);
            
            if (videoOpt.isPresent()) {
                Video video = videoOpt.get();
                List<Video> relatedVideos = videoService.getAllActiveVideos();
                
                // 移除当前视频
                if (relatedVideos != null) {
                    relatedVideos.removeIf(v -> v.getId().equals(id));
                } else {
                    relatedVideos = new ArrayList<>();
                }
                
                model.addAttribute("video", video);
                model.addAttribute("relatedVideos", relatedVideos);
                model.addAttribute("pageTitle", video.getTitle() + " - 视频播放");
                
                return "video-player";
            } else {
                model.addAttribute("error", "视频不存在");
                model.addAttribute("pageTitle", "视频不存在");
                return "error";
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "加载视频失败: " + e.getMessage());
            model.addAttribute("pageTitle", "加载失败");
            return "error";
        }
    }
    
    /**
     * 上传页面
     */
    @GetMapping("/upload")
    public String uploadPage(Model model) {
        model.addAttribute("pageTitle", "视频上传");
        return "upload";
    }
    
    /**
     * 搜索页面
     */
    @GetMapping("/search")
    public String searchPage(Model model) {
        model.addAttribute("pageTitle", "搜索视频");
        return "search";
    }
}
